using System;
using System.Xml.XPath;
using Searchtools.Summarizer;
using Searcher = SearchTools.Search.Searcher;
using ResultCollection = SearchTools.Search.ResultCollection;
using Result = SearchTools.Search.Result;
using Lucene.Net.Search;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using System.Xml;
using System.Collections;
using System.Reflection;


namespace SearchTools
{

	/// <summary>
	/// Container class for xsl extension methods
	/// </summary>
	public sealed class Xsl
	{
		/// <summary>
		/// Fetch resultset for query and return as nodeset
		/// </summary>
		/// <param name="query">Query string</param>
	    /// <param name="max">Maximum number of results</param>
		/// <returns>Result Xml</returns>
		public static XPathNodeIterator Search(string query,int max)
		{
			ResultCollection results = Searcher.Search(query,max);
			
			XmlDocument doc = new XmlDocument();
			XmlElement root = (XmlElement)doc.AppendChild(doc.CreateElement("root"));
			
			//Append Results
			foreach(Result result in results)
			{
			
				XmlElement elem = doc.CreateElement("Result");
				XmlElement score = doc.CreateElement("Score");
				score.InnerText = result.Score.ToString();
				elem.AppendChild(score);

				foreach(string name in result.Keys)
				{
					XmlElement resultField = doc.CreateElement("Field"); 
					XmlAttribute attr = doc.CreateAttribute("name","");
					attr.Value = name;
					resultField.InnerText = (string)result[name];
					resultField.Attributes.Append(attr);
					elem.AppendChild(resultField);
				}

				root.AppendChild(elem);
			}

			XPathDocument xdoc = new XPathDocument(new XmlNodeReader(doc));
			XPathNavigator nav = xdoc.CreateNavigator();
			return nav.Select("/root/*");
		}


        ///<summary>
        ///</summary>
        ///<param name="input"></param>
        ///<param name="sentences"></param>
        ///<returns></returns>
        public static string Summarise(string input,int sentences)
        {
            SimpleSummarizer summarizer = new SimpleSummarizer();

            return summarizer.Summarize(input, sentences);
        }
        
	}
}
